package br.com.sovi.gwt.client.controleunitario;

import java.util.Date;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.gwtext.client.widgets.form.DateField;
import com.gwtext.client.widgets.form.TimeField;

public class ControleUnitarioFiltroPanel extends VerticalPanel {

	private ListBox dispositivosBox;

	private DateField inicioDateField, terminoDateField;

	private TimeField inicioTimeField, terminoTimeField;

	private Button rotaButton;

	private final ActionListener actionListener;

	public ControleUnitarioFiltroPanel(ActionListener actionListener) {
		this.actionListener = actionListener;

		this.setSpacing(3);

		dispositivosBox = new ListBox(false);

		Date dataAtual = new Date();

		inicioDateField = new DateField("Início", "inicioDate", 80);
		inicioDateField.setFormat("d/m/y");
		inicioDateField.setValue(dataAtual);

		terminoDateField = new DateField("Término", "terminoDate", 80);
		terminoDateField.setFormat("d/m/y");
		terminoDateField.setValue(dataAtual);

		inicioTimeField = new TimeField();
		terminoTimeField = new TimeField();

		inicioTimeField.setAllowBlank(false);
		terminoTimeField.setAllowBlank(false);
		
		inicioTimeField.setValue("12:30");
		terminoTimeField.setValue("14:00");

		inicioTimeField.setHideLabel(true);
		inicioTimeField.setWidth(60);

		inicioTimeField.setFormat("H:i");
		terminoTimeField.setFormat("H:i");

		this.add(buildFormLabel("Dispositivos"));
		this.add(dispositivosBox);

		this.add(buildFormLabel("Início"));
		this.add(createDateColumn(inicioDateField, inicioTimeField));

		this.add(buildFormLabel("Término:"));
		this.add(createDateColumn(terminoDateField, terminoTimeField));

		this.add(initButtonRow());

		actionListener.updateDispositivos();
	}

	private HorizontalPanel initButtonRow() {
		HorizontalPanel panel = new HorizontalPanel();

		rotaButton = new Button("Gerar Rota", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				String dispositivo = dispositivosBox.getItemText(dispositivosBox.getSelectedIndex());

				Date inicioCalendar = gerarData(inicioDateField, inicioTimeField);
				Date terminoCalendar = gerarData(terminoDateField, terminoTimeField);

				if (GWT.isScript()) {
					GWT.log("dispositivo: " + dispositivo, null);
					GWT.log("inicial: " + inicioCalendar, null);
					GWT.log("final: " + terminoCalendar, null);
				}

				actionListener.gerarRota(dispositivo, inicioCalendar, terminoCalendar);
			}
		});

		panel.add(rotaButton);

		return panel;
	}

	private Label buildFormLabel(String string) {
		Label label = new Label(string);
		label.setStyleName("x-form-item");
		return label;
	}

	public HorizontalPanel createDateColumn(DateField df, TimeField tf) {
		HorizontalPanel vp = new HorizontalPanel();
		vp.setSpacing(0);
		vp.setStyleName("controleUnitarioFiltroPanel_dateColumn");

		vp.add(df);
		tf.setHideLabel(true);
		tf.setWidth(80);
		vp.add(tf);

		return vp;
	}

	protected void populateDispositivosBox(List<String> result) {
		for (int i = 0; i < dispositivosBox.getItemCount(); i++) {
			dispositivosBox.removeItem(0);
		}

		if (result != null || !result.isEmpty()) {
			for (String string : result) {
				dispositivosBox.addItem(string);
			}
		}
		else {
			Window.alert("Nenhum dispositivo encontrado.");
		}
	}

	private Date gerarData(DateField dataField, TimeField timeField) {
		Date value = dataField.getValue();
		value.setHours(Integer.parseInt(timeField.getText().split(":")[0]));
		value.setMinutes(Integer.parseInt(timeField.getText().split(":")[1]));
		return value;
	}

	interface ActionListener {

		void updateDispositivos();

		void gerarRota(String dispositivo, Date time, Date time1);

	}

}
